package com.lgkj.ui.manager;

import android.content.DialogInterface;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.lgkj.R;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.lgkj.utils.KeybordS;
import com.lgkj.utils.SharedPreferencesUtils;
import com.lgkj.utils.TitleBuilder;
import com.lgkj.view.SelectPicPopupWindow;

import org.devio.takephoto.app.TakePhotoActivity;
import org.devio.takephoto.model.TResult;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/4 0004.
 */

public class HaiSalonAddActivity extends TakePhotoActivity {

    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_sex)
    TextView etSex;
    @BindView(R.id.et_text)
    EditText etText;
    @BindView(R.id.ibtn_save)
    ImageButton ibtnSave;
    @BindView(R.id.itb_img)
    ImageButton itbImg;
    private SelectPicPopupWindow menuWindow;
    private String imageId = "";

    @Override
    public int getLayoutId() {
        return R.layout.activity_hair_salon_add;
    }

    @Override
    public void initView() {
        new TitleBuilder(this).setTitleText("造型师添加").setLeftIcoListening(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void initData() {

    }

    private View.OnClickListener itemsOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                KeybordS.hideKeyboard(HaiSalonAddActivity.this);
            } catch (Exception e) {
                Log.i(TAG, "onClick: " + e.getMessage());
            }
            menuWindow.dismiss();
            switch (v.getId()) {
                case R.id.btn_take_photo:
                    CommonUtils.takeFromCapture(getTakePhoto(), 258, 395);
                    break;
                case R.id.btn_pick_photo:
                    CommonUtils.takeFromDocuments(getTakePhoto(), 258, 395);
                    //调用系统图库的意图
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void takeCancel() {
        super.takeCancel();
    }

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        String url = CommonConstant.getTakeImagePath();
        Log.i(TAG, "showImg: " + url);
        upload(new File(url));
    }

    private void upload(File file) {
        showDiaLog("图片正在上传");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(HaiSalonAddActivity.this));
        HttpUtils.uploadImage(map, CommonConstant.uploadImg, file, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                toastLong("上传成功", 0);
                imageId = jsonObject.optString("id");
                Uri uri = Uri.fromFile(new File(CommonConstant.getTakeImagePath()));
                itbImg.setImageURI(uri);
            }

            @Override
            public void errorHttp(String msg) {
                toastLong("上传失败", 0);
            }
        });
    }

    private void save() {
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(HaiSalonAddActivity.this));
        map.put("barbershop_id", SharedPreferencesUtils.getParam(HaiSalonAddActivity.this, "shop_id", "").toString());
        map.put("hairstyle_name", etName.getText().toString());
        map.put("hairstyle_text", etText.getText().toString());
        map.put("hairstyle_type", sex);
        map.put("img_id", imageId);
        HttpUtils.postHttp(map, CommonConstant.hairAdd, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                toastLong(jsonObject.optString("msg"), 0);
                if (jsonObject.optInt("code") == 0) {
                    finish();
                }
            }

            @Override
            public void errorHttp(String msg) {

            }
        });
    }

    @OnClick({R.id.itb_img, R.id.ibtn_save, R.id.lv_sex})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.itb_img:
                menuWindow = new SelectPicPopupWindow(HaiSalonAddActivity.this, itemsOnClick);
////        menuWindow.sethideTake();
//                    //显示窗口
                menuWindow.showAtLocation(HaiSalonAddActivity.this.findViewById(R.id.main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                break;
            case R.id.ibtn_save:
                save();
                break;
            case R.id.lv_sex:
                chooiceSex();
                break;
        }
    }

    private String sex;

    private void chooiceSex() {
        AlertDialog.Builder builder = new AlertDialog.Builder(HaiSalonAddActivity.this);
        builder.setTitle("请选择性别");
        //    指定下拉列表的显示数据
        final String[] cities = {"保密", "男", "女"};

        //    设置一个下拉的列表选择项
        builder.setItems(cities, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                sex = which + "";
                etSex.setText(cities[which]);
            }
        });
        builder.show();
    }
}
